/*
 * contrib/btree_gist/btree_macaddr8.c
 */
#include "postgres.h"

#include "btree_gist.h"
#include "btree_utils_num.h"
#include "utils/builtins.h"
#include "utils/inet.h"

typedef struct
{
	macaddr8	lower;
	macaddr8	upper;
	/* 使结构体大小 = sizeof(gbtreekey16) */
} mac8KEY;

/*
** OID 操作
*/
PG_FUNCTION_INFO_V1(gbt_macad8_compress);
PG_FUNCTION_INFO_V1(gbt_macad8_fetch);
PG_FUNCTION_INFO_V1(gbt_macad8_union);
PG_FUNCTION_INFO_V1(gbt_macad8_picksplit);
PG_FUNCTION_INFO_V1(gbt_macad8_consistent);
PG_FUNCTION_INFO_V1(gbt_macad8_penalty);
PG_FUNCTION_INFO_V1(gbt_macad8_same);


static bool fc_gbt_macad8gt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2(macaddr8_gt, PointerGetDatum(fc_a), PointerGetDatum(fc_b)));
}
static bool fc_gbt_macad8ge(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2(macaddr8_ge, PointerGetDatum(fc_a), PointerGetDatum(fc_b)));
}

static bool fc_gbt_macad8eq(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2(macaddr8_eq, PointerGetDatum(fc_a), PointerGetDatum(fc_b)));
}

static bool fc_gbt_macad8le(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2(macaddr8_le, PointerGetDatum(fc_a), PointerGetDatum(fc_b)));
}

static bool fc_gbt_macad8lt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2(macaddr8_lt, PointerGetDatum(fc_a), PointerGetDatum(fc_b)));
}


static int fc_gbt_macad8key_cmp(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	mac8KEY    *fc_ia = (mac8KEY *) (((const Nsrt *) fc_a)->t);
	mac8KEY    *fc_ib = (mac8KEY *) (((const Nsrt *) fc_b)->t);
	int			fc_res;

	fc_res = DatumGetInt32(DirectFunctionCall2(macaddr8_cmp, Macaddr8PGetDatum(&fc_ia->lower), Macaddr8PGetDatum(&fc_ib->lower)));
	if (fc_res == 0)
		return DatumGetInt32(DirectFunctionCall2(macaddr8_cmp, Macaddr8PGetDatum(&fc_ia->upper), Macaddr8PGetDatum(&fc_ib->upper)));

	return fc_res;
}


static const gbtree_ninfo tinfo =
{
	gbt_t_macad8,
	sizeof(macaddr8),
	16,							/* sizeof(gbtreekey16) */
	fc_gbt_macad8gt,
	fc_gbt_macad8ge,
	fc_gbt_macad8eq,
	fc_gbt_macad8le,
	fc_gbt_macad8lt,
	fc_gbt_macad8key_cmp,
	NULL
};


/**************************************************
 * macaddr 操作
 **************************************************/



static uint64 fc_mac8_2_uint64(macaddr8 *fc_m)
{
	unsigned char *fc_mi = (unsigned char *) fc_m;
	uint64		fc_res = 0;
	int			fc_i;

	for (fc_i = 0; fc_i < 8; fc_i++)
		fc_res += (((uint64) fc_mi[fc_i]) << ((uint64) ((7 - fc_i) * 8)));
	return fc_res;
}



Datum gbt_macad8_compress(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_compress(fc_entry, &tinfo));
}

Datum gbt_macad8_fetch(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_fetch(fc_entry, &tinfo));
}

Datum gbt_macad8_consistent(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	macaddr8   *fc_query = (macaddr8 *) PG_GETARG_POINTER(1);
	StrategyNumber fc_strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	/* Oid		subtype = PG_GETARG_OID(3); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(4);
	mac8KEY    *fc_kkk = (mac8KEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;

	/* 由此函数服务的所有案例都是精确的 */
	*fc_recheck = false;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_BOOL(gbt_num_consistent(&fc_key, (void *) fc_query, &fc_strategy,
									  GIST_LEAF(fc_entry), &tinfo, fcinfo->flinfo));
}


Datum gbt_macad8_union(PG_FUNCTION_ARGS)
{
	GistEntryVector *fc_entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
	void	   *fc_out = palloc0(sizeof(mac8KEY));

	*(int *) PG_GETARG_POINTER(1) = sizeof(mac8KEY);
	PG_RETURN_POINTER(gbt_num_union((void *) fc_out, fc_entryvec, &tinfo, fcinfo->flinfo));
}


Datum gbt_macad8_penalty(PG_FUNCTION_ARGS)
{
	mac8KEY    *fc_origentry = (mac8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
	mac8KEY    *fc_newentry = (mac8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
	float	   *fc_result = (float *) PG_GETARG_POINTER(2);
	uint64		fc_iorg[2],
				fc_inew[2];

	fc_iorg[0] = fc_mac8_2_uint64(&fc_origentry->lower);
	fc_iorg[1] = fc_mac8_2_uint64(&fc_origentry->upper);
	fc_inew[0] = fc_mac8_2_uint64(&fc_newentry->lower);
	fc_inew[1] = fc_mac8_2_uint64(&fc_newentry->upper);

	penalty_num(fc_result, fc_iorg[0], fc_iorg[1], fc_inew[0], fc_inew[1]);

	PG_RETURN_POINTER(fc_result);
}

Datum gbt_macad8_picksplit(PG_FUNCTION_ARGS)
{
	PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
										(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
										&tinfo, fcinfo->flinfo));
}

Datum gbt_macad8_same(PG_FUNCTION_ARGS)
{
	mac8KEY    *fc_b1 = (mac8KEY *) PG_GETARG_POINTER(0);
	mac8KEY    *fc_b2 = (mac8KEY *) PG_GETARG_POINTER(1);
	bool	   *fc_result = (bool *) PG_GETARG_POINTER(2);

	*fc_result = gbt_num_same((void *) fc_b1, (void *) fc_b2, &tinfo, fcinfo->flinfo);
	PG_RETURN_POINTER(fc_result);
}
